//
//  SJDiscoveryContainerViewController.m
//  WSL
//
//  Created by CJ on 2018/2/6.
//  Copyright © 2018年 sanjuetech. All rights reserved.
//

#import "SJDiscoveryContainerViewController.h"
#import "SJDiscoveryViewController.h"
#import "SJSearchViewController.h"
#import "SJLabelCell.h"
#import "SJClassListApi.h"
#import "SJLabelItem.h"
#import "SJClassifyListViewController.h"

@interface SJDiscoveryContainerViewController () <UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) UIView *navigationBarView;
@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, strong) UIButton *selectedButton;
@property (nonatomic, strong) UIButton *searchButton;

@end

@implementation SJDiscoveryContainerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupUI];
    
    [self loadNewData];

}

- (void)setupUI {
    
    
    [self.view addSubview:self.containerView];
    self.containerView.sd_layout
    .leftSpaceToView(self.view, SJLayoutFrame(0))
    .topSpaceToView(self.view, SJLayoutFrame(0))
    .rightSpaceToView(self.view, SJLayoutFrame(0))
    .bottomSpaceToView(self.view, SJLayoutFrame(0));
    
    
    [self.view addSubview:self.navigationBarView];
    
    if (812 == SJScreenH) {
        self.navigationBarView.sd_layout
        .leftSpaceToView(self.view, SJLayoutFrame(0))
        .topSpaceToView(self.view, SJLayoutFrame(40))
        .heightIs(SJLayoutFrame(103))
        .widthIs(SJScreenW);
    } else {
        self.navigationBarView.sd_layout
        .leftSpaceToView(self.view, SJLayoutFrame(0))
        .topSpaceToView(self.view, SJLayoutFrame(0))
        .heightIs(SJLayoutFrame(103))
        .widthIs(SJScreenW);
    }
    
    
    [self.navigationBarView addSubview:self.searchButton];
    self.searchButton.sd_layout
    .leftSpaceToView(self.navigationBarView, SJMargin)
    .topSpaceToView(self.navigationBarView, SJMargin)
    .heightIs(SJLayoutFrame(39))
    .widthIs(SJLayoutFrame(361));
    
    CGFloat w = SJItemWH * 8 + SJMargin * 7;
    
    [self.navigationBarView addSubview:self.collectionView];
    self.collectionView.sd_layout
    .leftSpaceToView(self.navigationBarView, SJMargin)
    .topSpaceToView(self.searchButton, SJLayoutFrame(15))
    .heightIs(SJLayoutFrame(30))
    .widthIs(w);
    
}


/**
 *  添加子控制器
 */
- (void)setupChildVces {
    
    for (SJLabelItem *item in self.dataArray) {
        if ([item.ID isEqualToString:@"0"]) { // 推荐
            SJDiscoveryViewController *vc01 = [[SJDiscoveryViewController alloc] init];
            vc01.isDiscovery = @"1";
            [self addChildViewController:vc01];
        } else if ([item.ID isEqualToString:@"-1"]) { // 关注
            SJDiscoveryViewController *vc01 = [[SJDiscoveryViewController alloc] init];
            [self addChildViewController:vc01];
        } else {
            
            SJClassifyListViewController *vc01 = [[SJClassifyListViewController alloc] init];
            vc01.classify = item.ID;
            [self addChildViewController:vc01];
        }
        
    }
    
//    SJDiscoveryViewController *vc01 = [[SJDiscoveryViewController alloc] init];
//    vc01.isDiscovery = @"1";
//    [self addChildViewController:vc01];
//
//    SJDiscoveryViewController *vc02 = [[SJDiscoveryViewController alloc] init];
////    vc02.isDiscovery = @"1";
//    [self addChildViewController:vc02];
//
//    SJDiscoveryViewController *vc03 = [[SJDiscoveryViewController alloc] init];
//    vc03.isDiscovery = @"1";
//    [self addChildViewController:vc03];
}

#pragma mark - getter

- (void)searchAction {
    
    SJSearchViewController *searchVC = [SJSearchViewController new];
    [self.navigationController pushViewController:searchVC animated:YES];
}

- (void)selectedItem:(UIButton *)button {
    
    NSInteger index = button.tag;
    
    if (index == 2) {
        
        return;
    }
    
    if (index == 3) {
        // 搜索
        [self searchAction];
        return;
    }
    
    [self.containerView removeAllSubviews];
    
    UIViewController *vc = self.childViewControllers[index];
    // 如果子控制器的view已经在上面，就直接返回
    if (vc.view.superview) return;
    
    self.selectedButton.selected = NO;
    button.selected = YES;
    self.selectedButton = button;
    
    [self.containerView addSubview:vc.view];
    vc.view.sd_layout.spaceToSuperView(UIEdgeInsetsZero);
    
}

- (UIButton *)searchButton {
    
    if (!_searchButton) {
        
        _searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_searchButton setImage:[UIImage imageNamed:@"explore_icon_search"] forState:UIControlStateNormal];
        [_searchButton addTarget:self action:@selector(searchAction) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _searchButton;
}

- (UIView *)navigationBarView {
    
    if (!_navigationBarView) {
        
        _navigationBarView = [UIView new];
        _navigationBarView.backgroundColor = [UIColor clearColor];
        
    }
    return _navigationBarView;
}

- (UIView *)containerView {
    
    if (!_containerView) {
        
        _containerView = [UIView new];
        _containerView.backgroundColor = [UIColor clearColor];
        
    }
    return _containerView;
}


- (NSMutableArray *)dataArray {
    
    if (!_dataArray) {
        
        _dataArray = [[NSMutableArray alloc] init];
        
    }
    return _dataArray;
}

- (UICollectionView *)collectionView {
    
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        CGFloat h = SJLayoutFrame(30);
        CGFloat w = SJLayoutFrame(60);
        layout.minimumInteritemSpacing = SJLayoutFrame(32);
        layout.minimumLineSpacing = SJLayoutFrame(32);
        layout.itemSize = CGSizeMake(w, h);
        layout.sectionInset = UIEdgeInsetsMake(0, SJLayoutFrame(20) - SJMargin, 0, SJLayoutFrame(20) - SJMargin);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor clearColor];
        
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        
        [_collectionView registerClass:[SJLabelCell class] forCellWithReuseIdentifier:NSStringFromClass([SJLabelCell class])];
        
    }
    
    return _collectionView;
}


#pragma mark - UICollectionViewDataSource, UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    SJLabelCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([SJLabelCell class]) forIndexPath:indexPath];
    
    SJLabelItem *item = self.dataArray[indexPath.row];
    
    cell.item = item;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    for (SJLabelItem *item in self.dataArray) {
        item.isSelected = NO;
    }
    
    SJLabelItem *item = self.dataArray[indexPath.row];
    item.isSelected = YES;
    
    [self.collectionView reloadData];
    
    
    [self.containerView removeAllSubviews];
    
    
    UIViewController *vc = self.childViewControllers[indexPath.row];
    // 如果子控制器的view已经在上面，就直接返回
    if (vc.view.superview) return;
    
    [self.containerView addSubview:vc.view];
    vc.view.sd_layout.spaceToSuperView(UIEdgeInsetsZero);
}


#pragma mark - request

- (void)loadNewData {
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"token"] = [SJUserDefaults objectForKey:SJToken];
    parameters[@"rn"] = SJCountOfPage;
    parameters[@"pn"] = @"0";
    
    SJClassListApi *api = [[SJClassListApi alloc] initWithParameters:parameters];
    
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        
        if ([api.responseObject[@"result"][@"err"] intValue] == 0) {
            self.dataArray = [[NSArray modelArrayWithClass:[SJLabelItem class] json:api.responseObject[@"content"][@"data"]] mutableCopy];
            
        }
        
        
        SJLabelItem *item2 = [SJLabelItem new];
        item2.ID = @"-1";
        item2.name = @"关注";
        [self.dataArray insertObject:item2 atIndex:0];
        
        SJLabelItem *item1 = [SJLabelItem new];
        item1.ID = @"0";
        item1.name = @"推荐";
        [self.dataArray insertObject:item1 atIndex:0];
        
        [self.collectionView reloadData];
        
        [self setupChildVces];
        
        NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        
        [self collectionView:self.collectionView didSelectItemAtIndexPath:indexPath];
        
        
    } failure:^(YTKBaseRequest *request) {
        [self.collectionView reloadData];
    }];
    
}


@end
